import { AppName } from '../../app.config.js';
import path from 'path';
import fs from 'fs';
import puppeteer from '../../../../lib/puppeteer/puppeteer.js';
import yaml from 'js-yaml';
//插件根目录
const __dirname = path.resolve() + path.sep + 'plugins' + path.sep + AppName;
// 文件存放路径
export const __PATH = {
 //数据
};
let yamltype ={
    //yaml文件
    //示例:"1":__PATH.master,
}
let type ={
    //玩家文件
      "1":__PATH.player_path,
      "2":__PATH.equipment_path,
      "3":__PATH.bag_path,
}
let jsontype={
      "1":__PATH.json1,
      "2":__PATH.json2,
      "3":__PATH.json3,
}
/**
 *
 * @param num [1:player 2:equipment 3:bag]
 * @returns 
 */
export async function existplayer(num,usr_qq) {
    let exist_player;
    exist_player = fs.existsSync(`${type[num]}/${usr_qq}.json`);
    if (exist_player) {
      return true;
    }
    return false;
  }
  /**
 *
 * @param num [1:player 2:equipment 3:bag]
 * @returns 
 */
  export async function Read_player(num,usr_qq) {
    let playerPath =`${type[num]}/${usr_qq}.json`
    
    //根据长度判读是否是分身
    let dir = path.join(playerPath);
    let player = fs.readFileSync(dir, 'utf8', (err, data) => {
      if (err) {
        console.log(err);
        return 'error';
      }
      return data;
    });
    //将字符串数据转变成数组格式
    player = JSON.parse(player);
    return player;
  }
  /**
 *
 * @param num [1:曹魏 2:东吴 3:蜀汉]
 * @returns 
 */
  export async function Read_json(num) {
    let playerPath =`${jsontype[num]}`
    //根据长度判读是否是分身
    let dir = path.join(playerPath);
    let player = fs.readFileSync(dir, 'utf8', (err, data) => {
      if (err) {
        console.log(err);
        return 'error';
      }
      return data;
    });
    //将字符串数据转变成数组格式
    player = JSON.parse(player);
    return player;
  }
  /**
 *
 * @param num [1:player 2:equipment 3:bag]
 * @returns 
 */
  export async function Write_player(num,usr_qq, player) {
    let dir = path.join(type[num], `${usr_qq}.json`);
    let new_ARR = JSON.stringify(player, '', '\t');
    fs.writeFileSync(dir, new_ARR, 'utf8', err => {
      console.log('写入成功', err);
    });
    return;
  }
   /**
 *
 * @param num [1:曹魏 2:东吴 3:蜀汉]
 * @returns 
 */
   export async function Write_json(num,json) {
    let dir = path.join(jsontype[num]);
    let new_ARR = JSON.stringify(json, '', '\t');
    fs.writeFileSync(dir, new_ARR, 'utf8', err => {
      console.log('写入成功', err);
    });
    return;
  }
  //判断属性存在是否
  export async function isNotNull(obj) {
    return obj !== undefined && obj !== null;
  }
  //获取所有玩家的qq号(前提是玩家文件名是他们的qq号)
  export async function alluser() {
    let B = [];
    let A = fs
      .readdirSync(__PATH.player_path)
      .filter(file => file.endsWith('.json'));
    for (let item of A) B.push(item.substring(0, item.lastIndexOf('.')));
    return B;
  }
/**
 *
 * @param num [1:曹魏 2:东吴 3:蜀汉]
 * @returns 
 */
//判断json文件有没有
  export async function exist_json(num) {
    let exist_player;
    exist_player = fs.existsSync(`${guojia[num]}`);
    if (exist_player) {
      return true;
    }
    return false;
  }
  /**
 *
 * @param num [1:master]
 * @returns 
 */
  export async function Read_yaml(num){
    try {
      const fileContents = fs.readFileSync(yamltype[num], 'utf8');
      const data = yaml.load(fileContents);
      return data;
      // 现在你可以使用 data 变量访问 YAML 文件中的数据
    } catch (error) {
      console.log(error);
    }
  }
  export async function Write_yaml(num,wyaml){
    try {
      // 将修改后的数据转换为 YAML 格式
      const yamlData = yaml.dump(wyaml);
      // 写入 YAML 文件
      fs.writeFileSync(yamltype[num], yamlData, 'utf8');
      console.log('YAML 文件写入成功！');
    } catch (error) {
      console.log(error);
    }
  }
